Public Class Cifrado
    Private Shared CasePassword As Boolean = False
#Region "Encriptar"
    Public Shared Function EncriptarTexto(ByVal Texto As String, ByVal Clave As String) As String
        Dim i As Integer, c As Integer
        Dim cadena As String = String.Empty
        If Not CasePassword Then
            'Convert password to upper case
            'if not case-sensitive
            'Clave = UCase$(Clave)
            Clave = Clave.ToUpper.ToString
        End If
        'Encrypt string
        If Len(Clave) Then
            For i = 1 To Len(Texto)
                c = Asc(Mid$(Texto, i, 1))
                c = c + Asc(Mid$(Clave, (i Mod Len(Clave)) + 1, 1))
                cadena = cadena & Chr(c And &HFF).ToString
            Next i
        Else
            cadena = Texto
        End If
        Return cadena
    End Function
    Public Shared Function DesencriptarTexto(ByVal Texto As String, ByVal Clave As String) As String
        Dim i As Integer, c As Integer
        Dim cadena As String = String.Empty
        If Not CasePassword Then
            'Convert password to upper case
            'if not case-sensitive
            'Clave = UCase$(Clave)
            Clave = Clave.ToUpper.ToString
        End If
        'Decrypt string
        If Len(Clave) Then
            For i = 1 To Len(Texto)
                c = Asc(Mid$(Texto, i, 1))
                c = c - Asc(Mid$(Clave, (i Mod Len(Clave)) + 1, 1))
                cadena = cadena & Chr(c And &HFF).ToString
            Next i
        Else
            cadena = Texto
        End If
        Return cadena
    End Function
#End Region
    Public Shared Function ClaveDinamica(ByVal CodigoOperacion As Integer, ByVal Fecha As Long, ByVal Hora As Date) As Integer

        Dim intValue As Integer = 0

        If Fecha < 1 Or (Not IsDate(Hora)) Then
            Return intValue
        End If

        Dim dteFecha As Date = FechaHora.FechaDate(Fecha)

        Dim intAnio As Integer = dteFecha.Year
        Dim intMes As Integer = dteFecha.Month
        Dim intDia As Integer = dteFecha.Day

        Dim intHora As Integer = Hora.Hour
        Dim intMin As Integer = Hora.Minute
        Dim intSeg As Integer = Hora.Second

        Dim intValorF As Integer = 0
        Dim intValorH As Integer = 0

        intValorF = intAnio * intMes * intDia
        intValorH = (intHora * 3600) + (intMin * 60) + intSeg
        intValue = intValorF + intValorH + CodigoOperacion

        Return intValue

    End Function
End Class
Public Class Kerberos

#Region "Encriptar"
    Public Shared Function Encriptar(ByVal Clave As String, ByVal Value As String) As String
        Dim strClave As String = Clave
        Dim strValue As String = String.Empty
        Dim strCAR As String = String.Empty
        Dim strCodigo As String = String.Empty

        Dim intI As Integer = 1
        For intI = 1 To Len(Value)
            strCAR = Mid(Value, intI, 1)
            strCodigo = Mid(strClave, ((intI - 1) Mod Len(strClave)) + 1, 1)
            strValue = strValue & Right("0" & Hex(Asc(strCodigo) Xor Asc(strCAR)), 2)
        Next intI

        Return strValue
    End Function
#End Region

#Region "DesEncriptar"
    Public Shared Function DesEncriptar(ByVal Clave As String, ByVal Value As String) As String
        Dim strClave As String = Clave
        Dim strValue As String = String.Empty
        Dim strCAR As String = String.Empty
        Dim strCodigo As String = String.Empty

        Dim intI As Integer = 1
        Dim intJ As Integer = 1
        For intI = 1 To Len(Value) Step 2
            strCAR = Mid(Value, intI, 2)
            strCodigo = Mid(strClave, ((intJ - 1) Mod Len(strClave)) + 1, 1)
            strValue = strValue & Chr(Asc(strCodigo) Xor Val("&h" + strCAR))
            intJ = intJ + 1
        Next intI

        Return strValue
    End Function
#End Region

End Class
